package com.jp.controller;

import com.jp.vo.Member;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Iterator;

/**
 * @类名称 MemberController
 * @类功能描述
 * @作者 蒋正午
 * @日期 2019-09-10  0:05
 */
@Controller
public class MemberController extends BaseController{

    @RequestMapping(value = "/addMember",method = RequestMethod.POST)
    @ResponseBody
    public Member addMember(Member member){

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("出生日期:" + sf.format(member.getBirthday()));
        return member;
    }

    @RequestMapping(value = "/addMemberValidate", method = RequestMethod.POST)
    @ResponseBody // 别忘了增加验证注解
    public Object validationAddMember(@Valid  Member member, BindingResult bindResult){

            if(bindResult.hasErrors()){
                // 如果验证发现有错误
                Iterator<ObjectError> iter = bindResult.getAllErrors().iterator();
                while(iter.hasNext()){
                    ObjectError objError = iter.next();
                    System.out.println("code:"+ objError.getCode()+",message:" + objError.getDefaultMessage());
                }
                return bindResult.getAllErrors();
            }else{
                return member;
            }
    }

}
